<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {
	public function __construct(){
        parent::__construct();
        $this->load->model('info_m');
    }
	public function index(){
		//导航栏
		$this->navmenu();
		$search=isset($_POST['search'])?$_POST['search']:'';
		if($search!=''){
			$where='verify=1 && (title like "%'.$search.'%" or content like "%'.$search.'%")';
		}else{
			$where='verify=1';
		}
		$infolist=$this->info_m->info_list($where);
		$this->assign('ifnoidlist',$infolist);
		$this->assign('search',$search);
	   	$this->display('home/index.html');
	}
	public function ajax_infolist(){
		if(isset($_POST['seq'])){
			 $where=array();
			 if(isset($_POST['colid'])){
			 	//获取所有下级id
	            $columnlist=$this->column_m->selall(array(),'*','coltype');
	            $colidarr=$this->get_child_all($columnlist,$_GET['colid']);
				$where['colid']=array('in',$colidarr);
			 }
            if(isset($_POST['search'])){
              $search=$_POST['search'];
              $where['title']=array('like',$_POST['search']);
              $where['content']=array('like',$_POST['search'],'||');
            }else{
              $search='';
            }
			//获取默认文章
			$where['verify']=array('=',1);
			$where['seq']=array('<',$_POST['seq']);
			$infolist=$this->info_m->infolist($where);
			if(empty($infolist)){
				ajax_return(0,'没有数据了');
			}
			$this->assign('ifnoidlist',$infolist);
			$data=$this->fetch('home/ajax_infolist.html');
			ajax_return(1,'',$data);
		}else{
			ajax_return(0,'参数错误');
		}
	}
	//获取所有子集id
    public function get_child_all($arr,$pid=1,&$child = array()){
        $child[] = $pid;
        if(is_array($arr)){
          foreach($arr as $k=>$v){
              if($v['parentid'] == $pid){
                  $child = $this->get_child_all($arr,$v['colid'],$child);
              }
          }
      }
        return $child;
    }
	
}
